<script>
var pagecontent = "";

function rightcontent(url) {
    pagecontent = $("#pagecontent").html();
    $.ajax({
        type: "post",
        url: url,
        success: function(data) {
            $("#pagecontent").html(data);
        }
    });
}

function getallmenu(grade) {
    var texthtml = "";
    $.ajax({
        type: "post",
        url: "/menu/menus/getmenu?s=" + grade,
        success: function(data) {
            $("select[name='Pid']").empty();
            $.each(data, function(m, elem) {
                if (elem.text == '首页') {
                    return true;
                } else {
                    texthtml += "<option value='" + elem.id + "'>" + elem.text + "</option>";
                }

            })
            $("select[name='Pid']").append(texthtml);
        }
    });
}

function changeselect() {
    var s = $("select[name='Grade']").val();
    if (s == '2') {
        $("#url").css('display', 'none');
        $("#parentmenu").css('display', 'block');
        getallmenu(s);
    } else if (s == '3') {
        $("#parentmenu").css('display', 'block');
        $("#url").css('display', 'block');
        getallmenu(s);
    } else {
        $("#parentmenu").css('display', 'none');
        $("#url").css('display', 'none');
    }
}

//表格全选按钮
function allcheck(b) {
    var checkboxs = $("input[id^=" + b + "_check_]");
    var idscheck = '#allcheck_' + b
    for (jj = 0; jj < checkboxs.length; jj++) {
        $(checkboxs[jj]).attr("checked", $(idscheck).is(':checked'));
    }
}

//产生左边菜单
function getLeftmenu(id) {

    $.ajax({
        type: "post",
        url: '/menu/getleftmenus/' + id,
        success: function(data) {
            $("#leftmenus").html(data);
        }
    });
}


//菜单切换
// $('.ui.secondary.pointing.menu a.item')
$('.ui.inverted.menu a.item')
    .on('click', function() {
        $(this).addClass('active').siblings().removeClass('active');
        // console.log();
        id = $(this).attr('id');
        if (id != "home_1") {
            $("#home").empty();
            text = "<div class='four wide column' id='leftmenus'></div><div class='twelve wide column' id='pagecontent'></div>";
            $("#home").append(text);
            getLeftmenu(id);
        } else {
            $("#home").empty();
        }
    });


//搜索
function subsearch(b, code) {
    gopage(b, code, 1, "");
}

//分页函数
function gopage(b, code, pagenum, args) {
    var saveformdata = $("#form_responsive_" + code).html();
    var c = $("#tables_" + code);
    var formdata = $("#form_responsive_" + code);
    sousuo = $(formdata).serialize();
    $.ajax({
        type: "GET",
        syc: false,
        url: "/menu/getsearchpage?" + sousuo,
        data: {
            table_code: code,
            table_args: args,
            table_page: pagenum,
        },
        success: function(data) {
            $('.ui.dimmer.modals').remove();
            c.parent().html(data);
            $("#form_responsive_" + code).html(saveformdata);
            $.each(sousuo.split("&"), function(i, field) {
                var k = field.split("=");
                if (k[1].length != 0) {
                    $("input[name='" + k[0] + "']").val(k[1]);
                }
            });
        }
    });
}

function search(b) {
    $("#" + b).modal('show');
}

function saveform(url, formid) {
    $.ajax({
        type: "post",
        data: $("#" + formid).serialize(),
        url: url,
        success: function(data) {
            $("#pagecontent").html(data);
            // loadmenu(url);
        }
    });
}

function cancle() {
    $("#pagecontent").html(pagecontent);
}

function prompt(url, code) {
    var j = "del('" + url + "','" + code + "')";
    $("#delete").attr('onclick', j);
    $("#Prompt").modal('show');

}

function del(url, code) {
    var nodes = $("input[id^=" + code + "_check_]");
    console.log(nodes);
    var listnodes = '';
    nodes.each(function() {
        if ($(this).is(':checked')) {
            listnodes = listnodes + $(this).attr("value") + ','
        }
    });
    console.log(listnodes);
    listnodes = listnodes.substring(0, listnodes.length - 1);
    if (listnodes == "") {

    } else {
        $.ajax({
            type: "post",
            data: {
                ids: listnodes
            },
            url: url,
            success: function(data) {
                $("#pagecontent").html(data);
            }
        });
    }
}

function table_preview(url) {
    $("#preview").remove();
    $.ajax({
        type: "post",
        url: url,
        success: function(data) {
            $("body").append(data);
            $("#preview").modal('show');
        }
    });
}


</script>
